package net.avh4.scratch.rpg.battle;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;

public class BattleSystemTestbed extends Canvas {

	/**
	 * 1L: initial version 
	 */
	private static final long serialVersionUID = 1L;
	
	private Battle battle;
	
	public BattleSystemTestbed(Battle battle) {
		this.battle = battle;
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		List<Character> teamA = new ArrayList<Character>();
		List<Character> teamB = new ArrayList<Character>();
		try {
			teamA.add(new Character("images/hero.png"));
			teamB.add(new Character("images/lizarddemon.png"));
			BattleSystemTestbed ths = new BattleSystemTestbed(new Battle(teamA, teamB));
			frame.getContentPane().add(ths);
			frame.setSize(640, 480);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);
			ths.runBattle();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void runBattle() {
		Thread thread = new Thread() {

			@Override
			public void run() {
				while (! battle.hasEnded()) {
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					battle.next();
					BattleSystemTestbed.this.repaint();
				}
			}
			
		};
		thread.start();
	}

	@Override
	public void paint(Graphics g) {
		final Graphics2D g2 = (Graphics2D) g;
		List<Character> teamA = battle.getTeamA();
		for (Iterator<Character> it = teamA.iterator(); it.hasNext(); ) {
			Character c = it.next();
			c.draw(g2, 300, 50);
			StringBuilder sb = new StringBuilder("HP: ");
			sb.append(c.getStat("HP").toString());
			g2.drawString(sb.toString(), 300, 20);
		}
		List<Character> teamB = battle.getTeamB();
		for (Iterator<Character> it = teamB.iterator(); it.hasNext(); ) {
			Character c = it.next();
			c.draw(g2, 50, 50);
			StringBuilder sb = new StringBuilder("HP: ");
			sb.append(c.getStat("HP").toString());
			g2.drawString(sb.toString(), 50, 20);
		}
	}
	
}
